package com.doctorliang.cheng.addsub;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by CHENG on 2017/9/30.
 * 自定义删除增加
 */

public class NumberAddSubView extends LinearLayout implements View.OnClickListener {
    private final Context mcontext;
    private ImageView iv_sub;
    private ImageView iv_add;
    private TextView tv_count;

    private int value = 1;
    private int minValue = 1;
    private int maxValue = 10;
    public NumberAddSubView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //把布局和当前类形成整体
        View.inflate(context,R.layout.number_add_sub_layout,this);
        iv_sub = (ImageView) findViewById(R.id.iv_sub);
        iv_add = (ImageView) findViewById(R.id.iv_add);
        tv_count = (TextView) findViewById(R.id.tv_count);
        this.mcontext = context;

        int value = getValue();
        setValue(value);

        //设置点击事件
        iv_sub.setOnClickListener(this);
        iv_add.setOnClickListener(this);
    }

    public int getValue() {
        String vsluestr = tv_count.getText().toString().trim();
        if (!TextUtils.isEmpty(vsluestr)) {
             value = Integer.parseInt(vsluestr);
        }

        return value;
    }

    public void setValue(int value) {
        this.value = value;
        tv_count.setText(value+"");
    }

    public int getMinValue() {
        return minValue;
    }

    public void setMinValue(int minValue) {
        this.minValue = minValue;
    }

    public int getMaxValue() {
        return maxValue;
    }

    public void setMaxValue(int maxValue) {
        this.maxValue = maxValue;
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.iv_add) {
            //加
            addNumber();
        } else {
            //减
            subNumber();

        }
//        Toast.makeText(mcontext, "当前商品数=="+value, Toast.LENGTH_SHORT).show();
    }

    /**
     * 加
     */
    private void subNumber() {
        if (value>minValue) {
            value--;
        }
        setValue(value);
        if (onNumberChangeListener!=null) {
            onNumberChangeListener.onNumberChange(value);//把值传回去
        }
    }

    /**
     * 减
     */
    private void addNumber() {
        if (value<maxValue) {
            value++;
        }
        setValue(value);
        if (onNumberChangeListener!=null) {
            onNumberChangeListener.onNumberChange(value);//把值传回去
        }
    }

    /**
     * 当数量发生改变的时候回调
     */
    public interface OnNumberChangeListener{
        /**
         * 当数据变化时回调
         * @param value
         */
        public void onNumberChange(int value);
    }

    //接受外界实例化
    private OnNumberChangeListener onNumberChangeListener;
    //生成接口的set方法，供外部修改
    public void setOnNumberChangeListener(OnNumberChangeListener onNumberChangeListener) {
        this.onNumberChangeListener = onNumberChangeListener;
    }
}
